package com.tx.activemq._01_c_p._03_jms_msg;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.io.IOException;

/**
 * jms
 */
public class Head_Consumer_JMS {

    private static final String DEFAULT_URL = "tcp://192.168.254.30:61616";

    private static final String QUEUE_NAME = "queue_01";

    public static void main(String[] args) throws JMSException, IOException {
        System.out.println("****1号消费者***");
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(DEFAULT_URL);
        Connection connection = factory.createConnection();
        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue(QUEUE_NAME);
        MessageConsumer consumer = session.createConsumer(queue);

        consumer.setMessageListener(msg ->{
            if(msg != null && msg instanceof TextMessage){
                TextMessage textMessage = (TextMessage)msg;
                try {
                    System.out.println(textMessage.getText() + "--优先级：" + textMessage.getJMSPriority() + "---唯一id：" + textMessage.getJMSMessageID());
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });

        //挂起控制台
        System.in.read();

        consumer.close();
        session.close();
        connection.close();
    }
}